import Logger from "./logger";
import {
    EventTarget
} from './eventTarget';

const eventTarget = new EventTarget();

export function addEventListener(eventName, ack) {
    let event = (data) => {
        if(eventName !== 'publish_stream_status' && eventName !== 'subscribe_stream_status' && eventName !== 'remote_stream_status' && eventName !== 'active_audio_input_stream'){
            Logger.info('ivcs-js-sdk:addEventListener event with name', eventName, ' &data,', data, new Date().toISOString());
        }
        ack(data);
    }
    eventTarget.addEventListener(eventName, event);
    return event;
}
export function dispatchEvent(eventName, code, data) {
    var value = {
        type: eventName
    };
    if (code) {
        value.code = code;
    }
    if (data) {
        value.data = data;
    }
    if(eventName !== 'publish_stream_status' && eventName !== 'subscribe_stream_status' && eventName !== 'remote_stream_status' && eventName !== 'active_audio_input_stream'){
        Logger.info('ivcs-js-sdk:dispatch event', eventName, code, data);
    }
    eventTarget.dispatchEvent(value);
}

export function removeEventListener(eventName, eventImplementationFunction) {
    Logger.info('ivcs-js-sdk:removeEventListener event ', eventName);
    eventTarget.removeEventListener(eventName, eventImplementationFunction);
}

export const systemErrorCode = {
    publishLocalFailed: 1001,
    subscribeStreamFailed: 1002,
    localStreamEnd: 1003,
    subscribeStreamEnd: 1004,
    zmsServerDisconnected: 1005,
    amsSocketIoError: 1006,
    ccsSocketIoError: 1007
}

export const systemErrorName = 'systemError';
export const ccsErrorName = 'ccsError';